#include "../hle.h"

#include "__sceDisplay.h"
#include "../gfx/display.h"

static u32 topAddr;

u32 isVblank = 0;

u32 mode = 0;
u32 xres = 480;
u32 yres = 272;

void sceDisplayIsVblank()
{
    isVblank ^= 1;
    _log(INF, HLE, "%d = sceDisplayIsVblank()", isVblank);
    RETURN(isVblank);
}

void sceDisplaySetMode()
{
    mode = PARAM(0);
    xres = PARAM(1);
    yres = PARAM(2);

    _log(INF, HLE, "sceDisplaySetMode(%d, %d, %d)", mode, xres, yres);

    RETURN(0);
}

void sceDisplayGetMode()
{
    if (mem_isOk(PARAM(0)))
        mem_write32(PARAM(0), mode);
    if (mem_isOk(PARAM(1)))
        mem_write32(PARAM(1), xres);
    if (mem_isOk(PARAM(2)))
        mem_write32(PARAM(2), yres);

    _log(INF, HLE, "sceDisplayGetMode(%08x, %08x, %08x)", PARAM(0), PARAM(1), PARAM(2));

    RETURN(0);
}

void sceDisplaySetFrameBuf()
{
    u32 lineSize = PARAM(1);
    ScePixelFormat pixelSize = (ScePixelFormat)PARAM(2);
    s32 sync = PARAM(3);
    topAddr = PARAM(0);
    u32 framebuf = 0;

    _log(INF, HLE, "sceDisplaySetFrameBuf(topAddr = %08x, lineSize = %d, pixelSize = %d, sync = %d)", topAddr, lineSize, pixelSize, sync);

    framebuf = 0x04000000 | topAddr;
    if (!mem_isOk(framebuf))
        framebuf = 0x04000000;

    if (lineSize % 2 != 0 || pixelSize >= 4)
    {
        _log(ERR, HLE, "=> Bad line or pixel size");
        RETURN(-1);
        return;
    }

    display_sync(1);
    display_setFb(framebuf, lineSize, pixelSize);

    RETURN(0);
}

void sceDisplayGetFrameBuf()
{
    _log(INF, HLE, "sceDisplayGetFrameBuf()");
    RETURN(topAddr);
}

void sceDisplayWaitVblankStart()
{
    _log(TRC, HLE, "sceDisplayWaitVblankStart()");
    RETURN(0);
    __KernelVsync();
}

void sceDisplayWaitVblankStartCB()
{
    _log(TRC, HLE, "sceDisplayWaitVblankStartCB()");
    RETURN(0);
    __KernelVsync();
}

void sceDisplayWaitVblank()
{
    _log(TRC, HLE, "sceDisplayWaitVblank()");    
    RETURN(0);
    __KernelVsync();
}

void sceDisplayWaitVblankCB()
{
    _log(TRC, HLE, "sceDisplayWaitVblankCB()");
    RETURN(0);
    __KernelVsync();
}

void sceDisplayGetVcount()
{
    u32 vCount = __KernelGetVcount();
    _log(INF, HLE, "%d = sceDisplayGetVcount()", vCount);    
    RETURN(vCount);
}

void sceDisplayGetFramePerSec()
{
    _log(WRN, HLE, "60 (hack) = sceDisplayGetFramePerSec()");    
    RETURN(60);
}

